home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3869 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.5 KB  |  90 lines

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Copy constructing an already default constructed object
  5. Date: 26 Jan 1996 13:29:59 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4eal0n$hgq@dawn.mmm.com>
  8. References: <4e906b$stk@elaine32.Stanford.EDU>
  9. Reply-To: kjhopps@mmm.com
  10.  
  11. brien oberstein (brien@leland.Stanford.EDU) wrote:
  12. > I'd like to get some opinions on the best/cleanest way
  13. > to accomplish the following:
  14.  
  15. > I've got an object which has already been constructed
  16. > via its default constructor which just sets all pointers
  17. > to NULL.  Whats the best way to deep-copy into it?
  18.  
  19. [ example and discussion arriving at the conclusion that
  20.   overloaded operator=() is required ]
  21.  
  22. > //
  23. > // deep-copy =
  24. > //
  25. > A& A::operator =(const A& other)
  26. > {
  27. >   A empty;
  28. >   A tmp(other);
  29. >   memcpy(this, &tmp, sizeof(A));
  30. >   memcpy(&tmp, &empty, sizeof(A));
  31. >   return *this;
  32. > }
  33.  
  34.  
  35. > I'd like to know what people think of the solution I've reached.
  36. > I figure that this type of shit is common enough so there should 
  37. > be some widely accepted solution to this problem.  Or maybe its
  38. > not, but believe me that the situation does occur.
  39.  
  40. You have arrived at the correct conclusion -- that operator=() is
  41. what you are looking for.  However, the function you've coded has
  42. some problems.
  43.  
  44. First, you have to decide for yourself what the copy semantics of
  45. your class are -- deep or shallow.  Regardless of what you decide,
  46. I think that the copy constructor and the assignment operator
  47. should produce identical results.  Frequently one is written in
  48. terms of the other.  Ultimately they must do an exhaustive copy
  49. of each data member.  Using memcpy as above can overwrite
  50. compiler-generated data within the object and can have unpredictable
  51. results.
  52.  
  53. Partial example:
  54.  
  55. class A
  56. {
  57. public:
  58.     A(const A& src);
  59.     void operator=(const A& src);
  60. private:
  61.     char* str;
  62. };
  63.  
  64. A::A(const A& src)
  65.     : str(0)
  66. {
  67.     operator=(src);
  68. }
  69.  
  70. void A::operator=(const A& rhs)
  71. {
  72.     if (this != &rhs) {
  73.     delete [] str;
  74.     if (rhs.str == 0)
  75.         str = 0;
  76.     else {
  77.         str = new char[strlen(rhs.str) + 1];
  78.         strcpy(str, rhs.str);
  79.     }
  80.     }
  81. }
  82. --
  83. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  84. 3M Company                      phone:  (612) 737-4643
  85. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  86. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  87.     But 3M speaks for me -- I did not write the following line:
  88.  
  89. Opinions expressed herein are my own and may not represent those of 3M.
  90.